package com.glory.dgcpcpda.adapter;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.base.MyApp;
import com.glory.dgcpcpda.entity.ScrapEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_LogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ToastUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * FileName: DefectListAdapter
 * Author: XJH
 * Date: 2020/5/28 9:03
 * Description:不良代码集合
 * History:
 */
public class DefectListAdapter extends BaseQuickAdapter<ScrapEntity, BaseViewHolder> {

   private TextView tvTotal;
   private double totalNum = 0;

   public DefectListAdapter(@Nullable List<ScrapEntity> data,TextView textView) {
      super(R.layout.item_defect_list,data);
      this.tvTotal = textView;
   }

   @Override
   protected void convert(BaseViewHolder helper, ScrapEntity item) {
      helper.setIsRecyclable(false);
      helper.setText(R.id.tvDefectCode,item.getACTIONCODE());
      helper.setText(R.id.tvDefectDesc,item.getACTIONREASON());
      EditText etDefectNum = helper.getView(R.id.etDefectNum);
      ImageView ivDelete = helper.getView(R.id.ivDelete);
      etDefectNum.setText(item.getMAINQTY());
      setEditListener(etDefectNum,item);

      ivDelete.setOnClickListener(view -> {
         getData().remove(item);
         notifyDataSetChanged();
         int outputNum = Calculation();
         tvTotal.setText(outputNum + "");

      });

      etDefectNum.setOnFocusChangeListener((view, hasFocus) -> {
         if(hasFocus) {
         }else{
            try {
               etDefectNum.setText(item.getMAINQTY());
               if(etDefectNum.getText().toString().equals(""))
               {
                  etDefectNum.setText(0+"");
               }
               int outputNum = Calculation();
               if(outputNum<0) {
                  FreeApi_ToastUtils.showFreeToast("不良数量大于批次数量，请重新输入", MyApp.app, false, Toast.LENGTH_LONG);
                  etDefectNum.setText(0+"");
               }else{
                  tvTotal.setText(outputNum+"");
               }

            } catch (NumberFormatException e) {
               e.printStackTrace();
               FreeApi_ToastUtils.showFreeToast("计算异常，请输入合法数字", MyApp.app, false, Toast.LENGTH_LONG);
            }
         }
      });
   }

   public void setTotalNum(double mainQTY){
      this.totalNum = mainQTY;
   }

   private int Calculation() {
      int sum = 0;
      List<ScrapEntity> data = getData();

      if (data.size() <= 0){
         return 0;
      }

      for (ScrapEntity entity:data){
         sum += Integer.parseInt(entity.getMAINQTY().equals("") ? "0" : entity.getMAINQTY());
      }

      if (sum > totalNum){
         sum = -1;
      }

      return sum;
   }

   private void setEditListener(EditText editText, ScrapEntity item) {
      if (editText.getTag() instanceof TextWatcher) {
         editText.removeTextChangedListener((TextWatcher) editText.getTag());
      }

      TextWatcher watcher = new TextWatcher() {
         @Override
         public void beforeTextChanged(CharSequence s, int start, int count, int after) {

         }

         @Override
         public void onTextChanged(CharSequence s, int start, int before, int count) {

         }

         @Override
         public void afterTextChanged(Editable s) {
            try {
               item.setMAINQTY(s.toString());
               FreeApi_LogUtils.saveErrorMessages(getData().toString(),"afterTextChanged");
               int outputNum = Calculation();
               if(outputNum<0) {
                  FreeApi_ToastUtils.showFreeToast("不良数量大于批次数量，请重新输入", MyApp.app, false, Toast.LENGTH_LONG);
                  editText.setText(0+"");
                  item.setMAINQTY("0");
                  ScrapEntity.MainQTY mainQTY = new ScrapEntity.MainQTY("0");
                  List<ScrapEntity.MainQTY> data = new ArrayList<>();
                  data.add(mainQTY);
                  item.setQTYLIST(data);
               }else{
                  tvTotal.setText(outputNum+"");
                  item.setMAINQTY(s.toString());
                  ScrapEntity.MainQTY mainQTY = new ScrapEntity.MainQTY(s.toString());
                  List<ScrapEntity.MainQTY> data = new ArrayList<>();
                  data.add(mainQTY);
                  item.setQTYLIST(data);
               }
            } catch (NumberFormatException e) {
               e.printStackTrace();
            }
         }
      };

      editText.addTextChangedListener(watcher);
      editText.setTag(watcher);
   }
}
